package com.itheima.mapper;

import com.itheima.domain.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface UserMapper {
    @Insert("insert into user values (#{id},#{username},#{password})")
    void save(User user);
    @Delete("delete from user where id=#{id}")
    void delete(int id);
    @Update("update user set username=#{username},password=#{password} where id = #{id}")
    void update(User user);
    @Select("select * from user where id=#{id}")
    User findById(int id);
    @Select("select * from user")
    List<User> findAll();

    @Select("SELECT * FROM user")
    @Results({
            @Result(id = true ,column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "password", property = "password"),
            @Result(property = "orderList",javaType = List.class,
                    column = "id",
                    many = @Many(select = "com.itheima.mapper.OrderMapper.findByUid"))
    })
    List<User> findUserAndOrderAll();//一对多的注解写法

    @Select("SELECT * FROM user")
    @Results({
            @Result(id = true ,column = "id", property = "id"),
            @Result(column = "username", property = "username"),
            @Result(column = "password", property = "password"),
            @Result(property = "roleList",javaType = List.class,
                    column = "id",
                    many = @Many(select = "com.itheima.mapper.RoleMapper.findRoleByUId"))
    })
    List<User> findUserAndRoleAll();//多对多的注解写法,有中间表
}